namespace GemBox.Spreadsheet
{
    using System;

    public sealed class ExcelColumn : ExcelColumnRowBase
    {
        private CellRange cells;
        private int width;

        internal ExcelColumn(ExcelColumnCollection parent, ExcelColumn sourceColumn) : base(parent, sourceColumn)
        {
            this.width = -1;
            this.width = sourceColumn.width;
        }

        internal ExcelColumn(ExcelColumnCollection parent, int index) : base(parent, index)
        {
            this.width = -1;
        }

        public void AutoFit()
        {
            int length = 2;
            CellRangeEnumerator readEnumerator = this.Cells.GetReadEnumerator();
            while (readEnumerator.MoveNext())
            {
                CellRange mergedRange = readEnumerator.CurrentCell.MergedRange;
                if ((mergedRange == null) || (mergedRange.Width <= 1))
                {
                    object obj2 = readEnumerator.CurrentCell.Value;
                    if (obj2 != null)
                    {
                        foreach (string str in obj2.ToString().Split(new char[] { '\n' }))
                        {
                            if (str.Length > length)
                            {
                                length = str.Length;
                            }
                        }
                    }
                }
            }
            this.width = length * 340;
        }

        public void Delete()
        {
            foreach (ExcelRow row in base.Parent.Parent.Rows)
            {
                row.AllocatedCells.Delete(base.Index);
            }
            ((ExcelRowColumnCollectionBase) base.Parent).DeleteInternal(base.Index);
        }

        public void InsertCopy(int columnCount, ExcelColumn sourceColumn)
        {
            ExcelRowCollection rows = sourceColumn.Parent.Parent.Rows;
            int num = 0;
            foreach (ExcelRow row in base.Parent.Parent.Rows)
            {
                ExcelCellCollection allocatedCells = rows[num].AllocatedCells;
                ExcelCell sourceCell = null;
                if (allocatedCells.Count > sourceColumn.Index)
                {
                    sourceCell = allocatedCells[sourceColumn.Index];
                }
                row.AllocatedCells.Insert(base.Index, columnCount, sourceCell);
                num++;
            }
            ((ExcelColumnCollection) base.Parent).InsertInternal(base.Index, columnCount, sourceColumn);
        }

        public void InsertEmpty(int columnCount)
        {
            foreach (ExcelRow row in base.Parent.Parent.Rows)
            {
                row.AllocatedCells.Insert(base.Index, columnCount, null);
            }
            ((ExcelColumnCollection) base.Parent).InsertInternal(base.Index, columnCount, null);
        }

        public CellRange Cells
        {
            get
            {
                if (this.cells == null)
                {
                    this.cells = new CellRange(base.Parent.Parent, 0, base.Index, 0xfffff, base.Index);
                }
                return this.cells;
            }
        }

        internal override int Extent
        {
            get
            {
                return this.Width;
            }
        }

        public int Width
        {
            get
            {
                if (this.width != -1)
                {
                    return this.width;
                }
                return base.Parent.Parent.DefaultColumnWidth;
            }
            set
            {
                this.width = value;
            }
        }
    }
}
